home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / RCS / HardwareContext.cc,v < prev    next >
Text File  |  1989-06-21  |  9KB  |  459 lines

  1. head     3.3;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    grunwald:3.3; strict;
  6. comment  @@;
  7.  
  8.  
  9. 3.3
  10. date     89.06.21.10.14.37;  author grunwald;  state Exp;
  11. branches ;
  12. next     3.2;
  13.  
  14. 3.2
  15. date     89.02.20.15.34.57;  author grunwald;  state Exp;
  16. branches ;
  17. next     3.1;
  18.  
  19. 3.1
  20. date     88.12.20.13.48.49;  author grunwald;  state Exp;
  21. branches ;
  22. next     1.3;
  23.  
  24. 1.3
  25. date     88.10.30.13.04.28;  author grunwald;  state Exp;
  26. branches ;
  27. next     1.2;
  28.  
  29. 1.2
  30. date     88.09.28.22.13.25;  author grunwald;  state Exp;
  31. branches ;
  32. next     1.1;
  33.  
  34. 1.1
  35. date     88.09.18.16.42.23;  author grunwald;  state Exp;
  36. branches ;
  37. next     ;
  38.  
  39.  
  40. desc
  41. @@
  42.  
  43.  
  44. 3.3
  45. log
  46. @*** empty log message ***
  47. @
  48. text
  49. @// This may look like C code, but it is really -*- C++ -*-
  50. // 
  51. // Copyright (C) 1988 University of Illinois, Urbana, Illinois
  52. //
  53. // written by Dirk Grunwald (grunwald@@cs.uiuc.edu)
  54. //
  55. #include "HardwareContext.h"
  56. #include "CpuMultiplexor.h"
  57. #include "Thread.h"
  58. #include "assert.h"
  59. #include <stream.h>
  60.  
  61. //
  62. //    Many machines have the same (or very similar) stack format.
  63. //    The 68K & 32K are examples of such machines.
  64. //
  65. //    The const *registers* defines the number of additional longwords
  66. //    needed on the stack past the last frame pointer. This value needs
  67. //    to jibe with the one in HardwareContext-<arch>.s.
  68. //
  69.  
  70. #ifdef mc68000
  71. static const int registerQuads = (56 + 96)/4;
  72. typedef long Quad;
  73. #endif
  74.  
  75. #ifdef ns32000
  76. static const int registerQuads = 15;
  77. typedef long Quad;
  78. #endif
  79.  
  80. const long MagicStackMarker = 0x464f4f20;    // this says 'FOO '
  81.  
  82. HardwareContext::HardwareContext (int check, unsigned size)
  83. {
  84.     checkStackLimits = check;
  85.  
  86.     saved_fp = 0;
  87.     saved_sp = 0;
  88.     stackBase = 0;
  89.     stackEnd = 0;
  90.     stackMax = 0;
  91.     stackSize = 0;
  92.     stackCheck = 0;
  93.     stackMallocAt = 0;
  94.  
  95.     if (size > 0) {
  96.     stackSize = size;
  97.     stackMallocAt = new void *[ stackSize ];
  98.     //
  99.     // stackBase should point to the first writeable cell of the
  100.     // new stack.
  101.     //
  102.     if (StackGrows == stackGrowsDown) {
  103.         stackEnd = stackMallocAt;
  104.         stackBase = &stackMallocAt[stackSize-1];
  105.     }
  106.     else {
  107.         stackBase = stackMallocAt;
  108.         stackEnd = &stackMallocAt[stackSize-1];
  109.     }
  110.     }
  111. }
  112.  
  113. void
  114. HardwareContext::switchContext(HardwareContext *to)
  115. {
  116.     if ( checkStackLimits ) {    // check old context
  117.     checkStack();
  118.     }
  119.  
  120.     magicSwitchTo(to);
  121.  
  122.     if ( checkStackLimits ) {    // check new context
  123.     checkStack();
  124.     }
  125. }
  126.  
  127. void
  128. HardwareContext::stackOverflow()
  129. {
  130.     register unsigned depth = stackBase - getSp();
  131.     if (stackMax < depth) {
  132.     stackMax = depth;
  133.     }
  134.     if ( stackMax >= stackSize ) {
  135.     cerr << "\nStack overflow\n";
  136.     cerr << " getSp() = " << hex(long(getSp()));
  137.     cerr << " and stackBase = " << hex(long(stackBase)) << "\n";
  138.     cerr << *this << "\n";
  139.     cerr << "Current task is \n";
  140.     cerr << *(CurrentThread());
  141.     cerr << "\n";
  142.     cerr.flush();
  143.     }
  144.     assert( stackMax < stackSize );
  145.     assert( *stackCheck == MagicStackMarker );
  146. }
  147.  
  148. #if  defined(mc68000) || defined(ns32000)
  149.  
  150. StackGrowthDirection StackGrows = stackGrowsDown;
  151.  
  152. void
  153. HardwareContext::buildReturnFrame(void *returnThis, voidFuncP returnAddress)
  154. {
  155. //
  156. //    To build a thread, we return to the first address of startOff,
  157. //    which will use the current FP & SP to build a local context,
  158. //    and then call the user main.
  159. //
  160. //    startOff needs to have a valid frame. The return address for this
  161. //    frame is NULL, since we never exit procedure startOff.
  162. //
  163.  
  164.     stackCheck = (long *) stackBase;
  165.     Quad **stack = (Quad **) stackBase;
  166.     *(stack--) = (Quad *) MagicStackMarker;
  167.     *(stack--) = 0;            // return address
  168.     Quad **startOffFp = stack;
  169.     *(stack--) = 0;            // frame pointer
  170.  
  171. //
  172. //    Construct the stack frame that will be used in procedure startOff.
  173. //    startOff needs to know the value for *this*, which is passed in
  174. //    as the first parameter. It also gets the back-link FP for the
  175. //    last frame (build above).
  176. //
  177.  
  178.     *(stack--) = (Quad *) returnThis;
  179.     Quad **nullFp = stack;
  180.     *(stack--) = (Quad *) startOffFp;        // FP for previous frame
  181.  
  182. //
  183. //    Now build the stack frame that is used to return to startOff
  184. //
  185.  
  186.     *(stack--) = (Quad *) returnAddress;
  187.  
  188.     saved_fp = (void **) stack;
  189.  
  190.     *(stack) = (Quad *) nullFp;
  191.     stack -= registerQuads;    // number of Quadword registers we are saving
  192.     saved_sp = (void **) stack;
  193. }
  194. #endif
  195.  
  196. void
  197. HardwareContext::classPrintOn(ostream& s)
  198. {
  199.     s << "[HardwareContext] Stack spans " << hex(long(stackEnd));
  200.     s << " to " << hex(long(stackBase));
  201.     s << " used is  " << (stackMax) << " of " << stackSize << "\n";
  202.     s << "[HardwareContext] fp = " << hex(long(saved_fp));
  203.     s << " sp = " << hex(long(saved_sp));
  204.     long p = *( (long *) saved_fp );
  205.     s << " @@fp = " << hex(p) << "\n";
  206. }
  207. @
  208.  
  209.  
  210. 3.2
  211. log
  212. @Start using Gnu library heaps for schedulers
  213. @
  214. text
  215. @d23 1
  216. a23 1
  217. static const int registerQuads = 30;
  218. @
  219.  
  220.  
  221. 3.1
  222. log
  223. @Steay version
  224. @
  225. text
  226. @a31 1
  227.  
  228. d140 1
  229. a140 1
  230.     saved_fp = stack;
  231. d144 1
  232. a144 1
  233.     saved_sp = stack;
  234. @
  235.  
  236.  
  237. 1.3
  238. log
  239. @*** empty log message ***
  240. @
  241. text
  242. @d13 8
  243. a20 1
  244. static const long FOO = 0x464f4f20;    // this says 'FOO '
  245. d22 13
  246. d50 1
  247. a50 1
  248.     stackMallocAt = new void*[stackSize];
  249. d69 2
  250. a70 24
  251.     //
  252.     // Check the SP of the current context
  253.     //
  254.     if (checkStackLimits) {
  255.     register unsigned depth = stackBase - getSp();
  256.     if (stackMax < depth) {
  257.         stackMax = depth;
  258.     }
  259.     if ( stackMax >= stackSize ) {
  260.         cerr << "\nStack overflow\n";
  261.         cerr << " getSp() = " << hex(long(getSp()));
  262.         cerr << " and stackBase = " << hex(long(stackBase)) << "\n";
  263.         cerr << *this << "\n";
  264.         if (to != 0) {
  265.         cerr << " and were switching to ...\n";
  266.         cerr << *to << "\n"; 
  267.         }
  268.         cerr << "Current task is \n";
  269.         cerr << *(CurrentThread());
  270.         cerr << "\n";
  271.         cerr.flush();
  272.     }
  273.     assert( stackMax < stackSize );
  274.     assert( *stackCheck == FOO );
  275. d75 2
  276. a76 15
  277.     //
  278.     //    Do all of this again, but now in our new context
  279.     //
  280.     if (checkStackLimits) {
  281.     assert( *stackCheck == FOO );
  282.     register unsigned depth = stackBase - getSp();
  283.     if (stackMax < depth) {
  284.         stackMax = depth;
  285.     }
  286.     if ( stackMax >= stackSize ) {
  287.         cerr << "\nStack overflow\n";
  288.         cerr << *this << "\n";
  289.         cerr.flush();
  290.     }
  291.     assert( stackMax < stackSize );
  292. d80 20
  293. a99 8
  294. //
  295. //    Many machines have the same (or very similar) stack format.
  296. //    The 68K & 32K are examples of such machines.
  297. //
  298. //    The const *registers* defines the number of additional longwords
  299. //    needed on the stack past the last frame pointer. This value needs
  300. //    to jibe with the one in HardwareContext-<arch>.s.
  301. //
  302. a100 9
  303. #ifdef mc68000
  304. static const int registers = 8 + 6 + 8;
  305. #endif
  306.  
  307. #ifdef ns32000
  308. static const int registers = 7 + 8;
  309. #endif
  310.  
  311.  
  312. d118 2
  313. a119 2
  314.     void **stack = (void **) stackBase;
  315.     *(stack--) = (void *) FOO;
  316. d121 1
  317. a121 1
  318.     void **startOffFp = stack;
  319. d131 3
  320. a133 3
  321.     *(stack--) = (void *) returnThis;
  322.     void **nullFp = stack;
  323.     *(stack--) = startOffFp;        // FP for previous frame
  324. d139 1
  325. a139 1
  326.     *(stack--) = (void *) returnAddress;
  327. d143 2
  328. a144 2
  329.     *(stack) = (void *) nullFp;
  330.     stack -= registers;        // number of longword registers we are saving
  331. d147 1
  332. a147 1
  333. #endif mc68000
  334. @
  335.  
  336.  
  337. 1.2
  338. log
  339. @*** empty log message ***
  340. @
  341. text
  342. @d1 6
  343. d8 1
  344. a8 1
  345. #include "HardwareCpu.h"
  346. a42 7
  347.     }
  348. }
  349.  
  350. HardwareContext::~HardwareContext()
  351. {
  352.     if (stackMallocAt != 0) {
  353.     delete stackMallocAt;
  354. @
  355.  
  356.  
  357. 1.1
  358. log
  359. @Initial revision
  360. @
  361. text
  362. @d7 2
  363. d19 1
  364. d25 4
  365. d31 1
  366. a31 1
  367.         stackBase = &stackEnd[stackSize-1];
  368. d35 1
  369. a35 1
  370.         stackEnd = &stackBase[stackSize-1];
  371. d73 1
  372. d77 1
  373. d82 1
  374. a95 1
  375. #ifdef mc68000
  376. d97 2
  377. a98 1
  378. //    Stack creation routines for the Motorola 68000 family
  379. d100 3
  380. a102 7
  381.  
  382. StackGrowthDirection StackGrows = stackGrowsDown;
  383.  
  384. void
  385. HardwareContext::buildReturnFrame(void *returnThis, void *returnAddress)
  386. {
  387.     void **stack = stackBase;
  388. a103 14
  389. //    To build a thread, we return to the first address of startOff,
  390. //    which will use the current FP & SP to build a local context,
  391. //    and then call the user main.
  392. //
  393. //    startOff needs to have a valid frame
  394. //
  395.     *(stack--) = (void *) 0x464f4f20;    // this says 'FOO '
  396. //
  397. //    This false return address is where I'd go if I left
  398. //    procedure 'startOff' (which I never do).
  399. //
  400.     *(stack--) = 0;            // return address
  401.     void **startOffFp = stack;
  402.     *(stack--) = 0;            // frame pointer
  403. d105 3
  404. a107 6
  405. //
  406. //    Construct the stack frame that will be used in procedure startOff.
  407. //    We assume that the we need to pass the start-up routine a
  408. //    single parameter, which should be the value of *this* for
  409. //    the class we're returning to. This is typically a Thread class.
  410. //
  411. d109 3
  412. a111 7
  413.     *(stack--) = (void *) returnThis;
  414.     void **nullFp = stack;
  415.     *(stack--) = startOffFp;        // FP for previous frame
  416. //
  417. //    Now build the stack frame that is used to return to startOff
  418. //
  419.     *(stack--) = returnAddress;
  420. a112 1
  421.     saved_fp = stack;
  422. d114 1
  423. a114 2
  424.     *(stack) = (void *) nullFp;
  425.     stack -= (7 + 6 + 8);    // number of longword registers we're saving
  426. a115 7
  427.     saved_sp = stack;
  428.     stackMax = stackBase - saved_sp;
  429. }
  430. #endif mc68000
  431.  
  432. #ifdef ns32000
  433.  
  434. a120 1
  435.     void **stack = stackBase;
  436. d126 2
  437. a127 1
  438. //    startOff needs to have a valid frame
  439. d129 4
  440. a132 1
  441.     *(stack--) = (void *) 0x464f4f20;    // this says 'FOO '
  442. a136 1
  443. //    Construct the stack frame that will be used in procedure startOff
  444. d138 6
  445. d147 1
  446. a150 2
  447.     void *ptr = (void *) returnAddress;
  448.     *(stack--) = ptr;
  449. d152 2
  450. d157 1
  451. a157 2
  452.     stack -= (7 * 4 + 8 * 4);
  453.  
  454. d160 1
  455. a160 1
  456. #endif ns32000
  457. d170 2
  458. @
  459.